package com.zigong.app.http;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkInfo;
import android.os.Build;

import com.zigong.app.http.retrofit.RetrofitNetServer;

/**
 * Created by Ly on 2016/11/29.
 */
public class NetUtils implements NetServer {

    private static NetUtils instance;

    private NetServer netServerImpl;

    private INetAdapter netAdapter;

    private NetUtils(NetServer netServerImpl) {
        this.netServerImpl = netServerImpl;
        if (netServerImpl instanceof INetAdapter) {
            netAdapter = (INetAdapter) netServerImpl;
        }
    }

    public static NetUtils getInstance() {
        if (instance == null) {
//            instance = new NetUtils(new TestNetServer());
            instance = new NetUtils(new RetrofitNetServer());
        }
        return instance;
    }

    public NetServer getNetServerImpl() {
        return getInstance().netServerImpl;
    }

    public static INetAdapter getNetAdapter() {
        return getInstance().netAdapter;
    }

    @Override
    public <T> void get(String method, INetCallback<T> callback) {
        netServerImpl.get(method, callback);
    }

    @Override
    public <T> void post(String method, Params params, INetCallback<T> callback) {
        netServerImpl.post(method, params, callback);
    }

    @Override
    public void setContext(Context context) {
        netServerImpl.setContext(context);
    }

    @Override
    public boolean hasSetContext() {
        return netServerImpl.hasSetContext();
    }

    /**
     * 判断网络类型
     *
     * @param context
     * @return -1：没有网络  1：WIFI网络2：wap网络3：net网络
     */
    public static int GetNetype(Context context) {
        int netType = -1;
        ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo == null) {
            return netType;
        }
        int nType = networkInfo.getType();
        if (nType == ConnectivityManager.TYPE_MOBILE) {
            if (networkInfo.getExtraInfo().toLowerCase().equals("cmnet")) {
                netType = 3;
            } else {
                netType = 2;
            }
        } else if (nType == ConnectivityManager.TYPE_WIFI) {
            netType = 1;
        }
        return netType;
    }

    /**
     * 判断网络类型
     *
     * @param context
     * @return -1：没有网络  1：WIFI网络2：wap网络3：net网络
     */
    public static String GetNetName(Context context) {
        int netType = -1;
        ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo == null) {
            return "NONE";
        }
        return networkInfo.getTypeName();
    }

    /**
     * 判断wifi网络是否可用
     *
     * @param context
     * @return
     */
    public static boolean isWifiConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mWiFiNetworkInfo = null;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                Network[] networks = mConnectivityManager.getAllNetworks();
                if (networks != null && networks.length > 0) {
                    for (int i = 0; i < networks.length; i++) {
                        mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(networks[i]);
                        if (mWiFiNetworkInfo != null) {
                            if (mWiFiNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                                break;
                            } else {
                                mWiFiNetworkInfo = null;
                            }
                        }
                    }
                }
            } else {
                mWiFiNetworkInfo = mConnectivityManager
                        .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
            }
            if (mWiFiNetworkInfo != null) {
                return mWiFiNetworkInfo.isAvailable();
            }
        }
        return false;
    }
}
